import { Component, OnInit, OnDestroy } from '@angular/core'
import { Router, ActivatedRoute, Params } from '@angular/router'
import { take } from 'rxjs/operators'
import { ACLService, ACLCanType } from '@delon/acl'
import { I18NService } from '@core/i18n/i18n.service'
import { IBaseInfoColumn } from '@shared/interfaces/baseInfo'
import { TradeService } from '../trade-abstract.service'
import { DetailBaseClass } from '@shared/classes/DetailBaseClass'
import { PageService } from '@shared/services/page.service'
import { TransProcI18n } from '@shared/enums'
import { DataService } from '@shared/services/data.service'
import { I18NPrefixWithPipe } from '@shared/filters/i18n-prefix-with.pipe'
import { I18NChanCodePipe } from '@shared/filters/i18n-chan-code.pipe'
import { AmtFormatPipe } from '@shared/filters/amt-format.pipe'
import { ITrade, ECategoryUrl, ERevFlag, ETransType, EProcFlag, ECategory } from '@shared/interfaces/trade'
@Component({
  selector: 'app-trades-detail-ec',
  templateUrl: './detail.component.html',
})
export class TradesDetailECComponent extends DetailBaseClass<ITrade> implements OnInit, OnDestroy {
  firstColumns: IBaseInfoColumn[]
  secondColumns: IBaseInfoColumn[]
  /** 只有正向成功交易才显示 */
  isShowRefund = false

  constructor(
    public i18n: I18NService,
    public router: Router,
    public route: ActivatedRoute,
    public tradeService: TradeService,
    public pageService: PageService,
    private dataService: DataService,
    private i18nPrefixWithPipe: I18NPrefixWithPipe,
    private i18nChanCodePipe: I18NChanCodePipe,
    private amtFormatPipe: AmtFormatPipe,
    private aclService: ACLService,
  ) {
    super(pageService)
  }

  ngOnInit(): void {
    this.route.params.subscribe((params: Params) => {
      this.initData(params)
    })
  }

  ngOnDestroy(): void {
    this.dataService.resetTrade()
  }

  initData(params: Params): void {
    const orderNum = params['orderNum']
    const intStoreCode = params['intStoreCode']
    this.loading = true
    this.tradeService
      .getOne({ orderNum, intStoreCode }, ECategoryUrl.ec)
      .pipe(take(1))
      .subscribe(res => {
        this.renderTrade(res)
      })
  }

  renderTrade(trade: ITrade): void {
    this.detailData = trade
    this.checkIsShowRefund()
    this.initFirstColumns()
    this.initSecondColumns()
    this.initColumns()
    this.loading = false
    this.dataService.setTrade(trade)
  }

  initColumns(): void {
    this.initValue.call(this, this.firstColumns)
    this.initValue.call(this, this.secondColumns)
  }

  private initFirstColumns(): void {
    const { transAmt, currency } = this.detailData
    this.firstColumns = [
      {
        span: 6,
        label: 'TRANS_TIME',
        labelI18n: 'TRADE.TRANS_TIME',
        index: 'createdAt',
        type: 'date',
      },
      {
        span: 8,
        label: 'ORDER_NUM',
        labelI18n: 'TRADE.ORDER_NUM',
        index: 'orderNum',
      },
      {
        span: 6,
        index: 'transAmt',
        label: 'TRADE.TRANS_AMT',
        labelI18n: 'TRADE.TRANS_AMT',
        asyncValue: this.amtFormatPipe.transform(transAmt, currency),
      },
      {
        span: 4,
        index: 'procFlag',
        label: 'GENERAL.INFO.STATUS',
        labelI18n: 'GENERAL.INFO.STATUS',
        pipe: this.parseProcFlag.bind(this),
      },
    ]
  }

  private initSecondColumns(): void {
    this.secondColumns = [
      {
        span: 12,
        label: 'CHANNEL',
        labelI18n: 'GENERAL.INFO.CHANNEL',
        index: 'chanCode',
        displayValue: this.i18nChanCodePipe.transform(this.detailData.chanCode.toUpperCase()),
      },
      {
        span: 12,
        label: 'BUSICD',
        index: 'busicd',
        labelI18n: 'BIZ.ACQUIRER.OTHER.TRADE.TYPE',
        displayValue: this.i18nPrefixWithPipe.transform(this.detailData.busicd),
      },
    ]
  }

  private parseProcFlag(procFlag: string): string {
    return this.trans(TransProcI18n[procFlag])
  }

  computeIsShowRefundButton(): boolean {
    const canRefund: ACLCanType = {
      ability: ['api.post.camel.trade.rfdp', 'api.post.camel.trade.refd'],
    }
    return this.aclService.canAbility(canRefund) && this.isShowRefund
  }

  private trans(str: string): string {
    if (!str) {
      return ''
    }

    return this.i18n.fanyi(str)
  }

  checkIsShowRefund(): void {
    this.isShowRefund =
      this.detailData.transType === ETransType.PayTrans &&
      this.detailData.procFlag === EProcFlag.ProcSuccess &&
      (this.detailData.revFlag === ERevFlag.RevNormal || this.detailData.revFlag === ERevFlag.RevPartRefunded)
        ? true
        : false
  }

  goManualRefund(): void {
    const url = this.router.url
    const originUrl = url.split(';')[0]
    this.router.navigateByUrl(`${originUrl}/manualRefund`)
  }
}
